package com.it.commicity.config.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.it.commicity.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
@Slf4j
@Component
public class MybatisMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("开始插入填充...");
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "createBy", String.class, UserHolder.getUser().getUserName());
    }


    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("开始更新填充...");
//        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
//        this.strictUpdateFill(metaObject, "updateBy", String.class, UserHolder.getUser().getUserName());
        // 强制更新（不检查原值）
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateBy", UserHolder.getUser().getUserName(), metaObject);
    }
}
